namespace Vwf.Tools {
	public class UString : Object {
		public string str;
		public int length { 
			get {
				return str.char_count();
			}
		}
		
		public string substring(int start, int size = -1) {
			if (size == -1) {
				size = length - start;
			};
			int ustart = str.index_of_nth_char(start);
			int ustop = str.index_of_nth_char(start + size);
			
			return str.substring(ustart, (ustop - ustart));
		}
		
		public string charAt(int pos) {
			return substring(pos, 1);
		}
		
		public int indexOf(string needle, int startPos = 0) 
		{
			return nCharOfIndex(str.index_of(needle, str.index_of_nth_char(startPos)));
		}
		
		public int indexOfNChar(int pos) {
			return str.index_of_nth_char(pos);
		}
		
		public int nCharOfIndex(int pos) {
			int i = pos;
			do {
				// stdout.printf("\t" + indexOfNChar(i).to_string() + " == " + pos.to_string() + "\n");
				if (indexOfNChar(i) <= pos) return i;
				i--;
			} while(true);
			// return 0;
		}
		
		public UString(string iStr = "") {
			str = iStr;
		}
	}
}
